﻿using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.Order;
using Shop.Model.View.Admin.Order;
using Shop.Model.View.Common;

namespace Shop.AdminApi.Controllers;

/// <summary>
/// 订单管理服务
/// </summary>
[ApiController]
[Route("admin/[controller]")]
public class OrdController: ShopControllerBase
{
    private readonly IOrdService _ordService;
    private readonly IUserContext _userContext;
    private readonly IAliyunAppService _AliyunAppService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ordService"></param>
    /// <param name="userContext"></param>
    /// <param name="AliyunAppService"></param>
    public OrdController(IOrdService ordService,IUserContext userContext,IAliyunAppService AliyunAppService)
    {
        _ordService = ordService;
        _userContext = userContext;
        _AliyunAppService = AliyunAppService;
    }

    /// <summary>
    /// 订单列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrderList")]
    public AppResult<PageModel<AOrderListVo>> OrderList([FromQuery]OrderListDto dto)
        => _ordService.OrderList(dto);

    /// <summary>
    /// 支付方式(常量)
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrdPayChannel")]
    public AppResult<List<PayChannelVo>> OrdPayChannel()
    {
        var payChannelVo = new List<PayChannelVo>();
        var payChannelOne = new PayChannelVo(){Key = 1, Name = "钱包支付", Sort = 0};
        var payChannelTwo = new PayChannelVo(){Key = 2, Name = "微信支付", Sort = 1};
        var payChannelThree = new PayChannelVo(){Key = 3, Name = "线下支付", Sort = 2};
        payChannelVo.Add(payChannelOne);
        payChannelVo.Add(payChannelTwo);
        payChannelVo.Add(payChannelThree);
        return payChannelVo;
    }

    /// <summary>
    /// 订单类型(常量)
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrderType")]
    public AppResult<List<OrderTypeVo>> OrderType()
    {
        var orderTypeVo = new List<OrderTypeVo>();
        var orderTypeOne = new OrderTypeVo(){Key = 1, Name = "普通订单", Sort = 0};
        var orderTypeTwo = new OrderTypeVo(){Key = 2, Name = "升级订单", Sort = 1};
        var orderTypeThree = new OrderTypeVo(){Key = 3, Name = "后台下单", Sort = 2};
        orderTypeVo.Add(orderTypeOne);
        orderTypeVo.Add(orderTypeTwo);
        orderTypeVo.Add(orderTypeThree);
        return orderTypeVo;
    }

    /// <summary>
    /// 订单详情
    /// </summary>
    /// <param name="oId"></param>
    /// <returns></returns>
    [HttpGet("Details")]
    public AppResult<OrderDetailsVo> OrderDetails([FromQuery] long oId)
        => _ordService.OrderDetails(oId);
    
    /// <summary>
    /// 订单状态
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrderStatus")]
    public AppResult<List<OrderStatusVo>> OrderStatus()
    {
        var orderStatusVo = new List<OrderStatusVo>();
        var orderStatusInfoOne = new OrderStatusVo(){Status = -2, Name = "已退款", Sort = 0};
        var orderStatusInfoTwo = new OrderStatusVo(){Status = -1, Name = "已取消", Sort = 1};
        var orderStatusInfoThree = new OrderStatusVo(){Status = 1, Name = "未付款", Sort = 2};
        var orderStatusInfoFour = new OrderStatusVo(){Status = 2, Name = "已付款", Sort = 3};
        var orderStatusInfoFive = new OrderStatusVo(){Status = 3, Name = "已发货", Sort = 4};
        var orderStatusInfoSix = new OrderStatusVo(){Status = 4, Name = "已签收", Sort = 5};
        var orderStatusInfoSeven = new OrderStatusVo(){Status = 5, Name = "已评价", Sort = 6};
        orderStatusVo.Add(orderStatusInfoOne);
        orderStatusVo.Add(orderStatusInfoTwo);
        orderStatusVo.Add(orderStatusInfoThree);
        orderStatusVo.Add(orderStatusInfoFour);
        orderStatusVo.Add(orderStatusInfoFive);
        orderStatusVo.Add(orderStatusInfoSix);
        orderStatusVo.Add(orderStatusInfoSeven);
        return orderStatusVo;
    }
    
    /// <summary>
    /// 导出订单
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("ExportToExcel")]
    public  IActionResult ExportToExcel(ExportToExcelDto dto)
    {
        var data = _ordService.ExportToExcel(dto);
        var fileName = "商城订单" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx";
        return File(data, "application/vnd.ms-excel", fileName);
    }

    /// <summary>
    /// 订单导出列
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrderFieldList")]
    public AppResult<List<OrderFieldListVo>> OrderFieldList()
        => _ordService.OrderFieldList();

    /// <summary>
    /// 计算行家业绩
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("ExportPerformance")]
    public IActionResult ExportPerformance(ExportPerformanceDto dto)
    {
        var data = _ordService.ExportPerformance(dto);
        var fileName = $"{dto.StartTime.Date}到{dto.EndTime.Date}行家业绩.xlsx";
        return File(data, "application/vnd.ms-excel", fileName);
    }

    
    /// <summary>
    /// 订单商品
    /// </summary>
    /// <returns></returns>
    [HttpGet("OrdGoods")]
    public AppResult<List<OrdGoodsVo>> OrdGoods([FromQuery] long oId)
        => _ordService.OrdGoods(oId);
    
    /// <summary>
    /// 标记发货
    /// </summary>
    /// <returns></returns>
    [HttpPost("Shipments")]
    public Task<AppResult> Shipments(List<ShipmentsDto> dto)
        => _ordService.Shipments(dto);
    
    /// <summary>
    /// 取消订单
    /// </summary>
    /// <param name="oId"></param>
    /// <returns></returns>
    [HttpPost("CancelOrd")]
    public AppResult CancelOrd(long oId)
        => _ordService.CancelOrd(oId, _userContext.Id);
    
    /// <summary>
    /// 地址库
    /// </summary>
    /// <returns></returns>
    [HttpGet("AddArea")]
    public AppResult<List<AddAreaVo>> AddArea([FromQuery] string dto)
        => _ordService.AddArea(dto);
    
    /// <summary>
    /// 商品列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("GoodsList")]
    public AppResult<List<GoodsListVo>> GoodsList([FromQuery]string? gName)
        => _ordService.GoodsList(gName);
    
    /// <summary>
    /// 创建订单
    /// </summary>
    /// <returns></returns>
    [HttpPost("AddOrder")]
    public async Task<AppResult> AddOrder(AddOrderDto dto)
        => await _ordService.AddOrder(dto, _userContext.Id);
    
    /// <summary>
    /// 获取Oss文件上传签名
    /// </summary>
    /// <returns></returns>
    [HttpGet("OssSign")]
    public AppResult<OssSignDto> GetOssSign()
        =>_AliyunAppService.GetOssSign(string.Empty);
    
}